package com.eveningoutpost.dexdrip.watch.thinjam.assets;

import com.eveningoutpost.dexdrip.Models.JoH;
import com.eveningoutpost.dexdrip.Models.UserError;

import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;

import lombok.val;

// jamorham

class DigitalSignature {

    private static final String ASSET_SIGNING_PUBLIC_KEY = "308203423082023506072A8648CE3804013082022802820101" +
            "00AEBF442A40BBC39FBACEEEED403EC0FECCD20B97400822D12711ADAC8B194F3C983161A4D5E73C3C41A335BCD4BE089" +
            "6116ACF4C20781CD57CBFBCEEAE13187B8E5E3AE81768644C6F2A45C2B994B9BC2CAD43184F8508D9A01D1B09A3D490C3" +
            "8C653784DE8AB13E628F8811212811D662423B9CF19A827990E04573B371A642428CA51FA8E30767A11FF9B6B46D476B1" +
            "7BD41472E51DF83AF1179331EBD5DDDCA072C716E46CFD2AAD12860ECC27AACE1DFF2DA001DA010CCF22C24CDEEDA2FFB" +
            "272700EE58581BE66C9CE95D463E4C966BC862A7B718BEF416796090D5E5ADB637A53F4CF3A47AF913293CFF8374C62EC" +
            "10565C30F2F47137B47248A0F8AAF021D00DD099D3D0DC20A73548EF17DD164EAF2630BD001D770582741CE8513028201" +
            "00629E23F2F933FAC8D643FF0A079B8CB67440DFD1DACD88EC8ED03DE50DE68CF7D93B057ED0990BCFEFDF227254296EB" +
            "51097B10885B71769660FA66BFA692BD144C7FE0CCB912C5534D06F05C66880CD5A7439CD7EFAE9B029957BBFD5D8573E" +
            "41AA203EB50D62B3F5F1E5143D2E1477B9EF30F560F70099291216F4F9272CA40B3026047414BA857D11B0ECBE1343DEF" +
            "AE2712D6D4DBC72930317D737EA985488AFA7496C62F75C121F613C59FD8A6F9AA8FD6F6131C5EE2E60BBE48E94BBA557" +
            "844B4060B7DCFFEF691EFFDC68BC1F3F69D3B86EFBA48FFC1FDADCA3BA413B460771FE0C52C85A116E304DED9473BE08F" +
            "2F033049FFEFE470EC8CB28EC8B5803820105000282010072497D618E013E29446E7A14D23479F74A43D5C64A02368AF5" +
            "E547CEAFAA2314E1340A1EF22776F4D37DAC366A1C0EE148675EAACC047593DA04BF104E08C28A72726F910DE6E2F169F" +
            "C2D91A80F06FB6276C651A332C5F5E180DBC5AE12478AFFC7F7A8DFACAFBE0F109FA34ECB80D3FC6F96E2C7CE7271FFCC" +
            "542A7721887C79686E2A01217DD26CA47EDA7C55A9785AD3DF00E51BAC3D12055645346C11C1ACEF7093CE7D1D4CEE3C6" +
            "AE41B8F2C21B35647A291C0BFA59E7E103A57AFCDC5590E597833B15C91809D5322077E9278854627172FE1918035EA1A" +
            "AE95BED7DC2B49D483F254764F9F20F83D21D190702AD3071A18184B87F3F8F4C12CE76C79";


    private static final String TAG = "AssetSignature";

    private static PublicKey getPublicKey() {
        try {
            return KeyFactory.getInstance("DSA")
                    .generatePublic(new X509EncodedKeySpec(JoH.hexStringToByteArray(ASSET_SIGNING_PUBLIC_KEY)));
        } catch (Exception e) {
            UserError.Log.e(TAG, "Failed to get public key: " + e);
            return null;
        }
    }

    static boolean firstCheck(final byte[] data, final byte[] signature) {
        try {
            val signCheck = Signature.getInstance("SHA256withDSA");
            signCheck.initVerify(getPublicKey());
            signCheck.update(data);
            return signCheck.verify(signature);
        } catch (Exception e) {
            UserError.Log.e(TAG, "Verification failed due to exception: " + e);
            return false;
        }
    }

}


